﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using SisTransportes.Apresentacao.WebForm.Config;

namespace SisTransportes.Apresentacao.WebForm.Setting
{
    public class Display : Configuracao
    {


        /// <summary>
        /// Renderiza Imagem da logo caso esteja configurada
        /// </summary>
        /// <param name="imgLogo">Web control Image</param>
        public static void LogoHeader(System.Web.UI.WebControls.Image imgLogo)
        {
            imgLogo.ImageUrl = Configuracao.Logo;
            imgLogo.AlternateText = Configuracao.TituloApp;
            imgLogo.Attributes.Add("title", Configuracao.TituloApp);
            imgLogo.Width = Configuracao.LogoLarg;
            imgLogo.Height = Configuracao.LogoAlt;
        }



        /// <summary>
        /// Lista menu principal no controle Menu com base no arquivo Menu.xml
        /// </summary>
        /// <param name="Menu">Controle Menu</param>
        public static void MenuPrincipal(System.Web.UI.WebControls.Menu Menu)
        {
            try
            {
                XElement xml = XElement.Load(Configuracao.PathAplicacao + Configuracao.PathXmlMenu);

                List<object> listXml = new List<object>();
                foreach (XElement default_menu in xml.Elements())
                {
                    foreach (XElement menu in default_menu.Elements())
                    {
                        System.Web.UI.WebControls.MenuItem itemMenu = new System.Web.UI.WebControls.MenuItem();
                        string x_name = menu.Attribute("name").Value;
                        string x_url = menu.Attribute("url").Value;
                        string nome = App_GlobalResources.Config.ResourceManager.GetString(x_name);
                        itemMenu.Text = nome;
                        itemMenu.NavigateUrl = x_url;

                        if (menu.HasElements)
                        {
                            foreach (XElement submenu in menu.Elements())
                            {
                                System.Web.UI.WebControls.MenuItem itemSubMenu = new System.Web.UI.WebControls.MenuItem();
                                ArrayList array_name = Funcoes.SplitString_ArrayList('%', submenu.Attribute("name").Value);
                                itemSubMenu.Text = App_GlobalResources.Config.ResourceManager.GetString(array_name[0].ToString()) + " " + App_GlobalResources.Config.ResourceManager.GetString(array_name[1].ToString());
                                itemSubMenu.NavigateUrl = submenu.Attribute("url").Value;

                                itemMenu.ChildItems.Add(itemSubMenu);
                            }
                        }

                        Menu.Items.Add(itemMenu);
                    }
                }

            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// Lista ícones de menu no dashboard com base no xml Menu.xml e na configuração dos itens a serem determinados no array
        /// configurado no web.config
        /// </summary>
        /// <param name="ListView">Web control ListView</param>
        /// <param name="array_excessoes">Array de string</param>
        public static void MenuIcones(System.Web.UI.WebControls.ListView ListView, string[] array_excessoes)
        {
            try
            {
                XElement xml = XElement.Load(Configuracao.PathAplicacao + Configuracao.PathXmlMenu);

                string[] _excessoes = array_excessoes;

                List<object> listXml = new List<object>();
                foreach (XElement default_menu in xml.Elements())
                {
                    foreach (XElement menu in default_menu.Elements())
                    {
                        string x_name = menu.Attribute("name").Value;
                        string x_url = Config.Configuracao.HttpHost + menu.Attribute("url").Value;
                        string x_icone = menu.Attribute("icone").Value;

                        string nome = App_GlobalResources.Config.ResourceManager.GetString(x_name);

                        object object_list = new { Nome = nome, Url = x_url, Icone = x_icone };

                        for (int i = 0; i < array_excessoes.Count(); i++)
                        {
                            if (array_excessoes[i] == x_name)
                            {
                                listXml.Add(object_list);
                                break;
                            }
                        }

                    }

                    ListView.DataSource = listXml;
                    ListView.DataBind();
                }

            }
            catch
            {
                throw;
            }


        }



        /// <summary>
        /// Lista submenu de acordo com a sessao especificada
        /// </summary>
        /// <param name="ListView">control listview</param>
        /// <param name="sessao">string nome da sessao</param>
        public static void MenuLateral(System.Web.UI.WebControls.ListView ListView, string sessao)
        {
            try
            {
                XElement xml = XElement.Load(Configuracao.PathAplicacao + Configuracao.PathXmlMenu);

                List<object> listXml = new List<object>();

                foreach (XElement default_menu in xml.Elements())
                {
                    foreach (XElement menu in default_menu.Elements())
                    {
                        foreach (XElement submenu in menu.Elements())
                        {
                            if (menu.Attribute("name").Value == sessao)
                            {
                                ArrayList array_name = Funcoes.SplitString_ArrayList('%', submenu.Attribute("name").Value);
                                string x_nome = App_GlobalResources.Config.ResourceManager.GetString(array_name[0].ToString()) + " " + App_GlobalResources.Config.ResourceManager.GetString(array_name[1].ToString());
                                string x_url = Config.Configuracao.HttpHost  + submenu.Attribute("url").Value;

                                object object_list = new { Nome = x_nome, Url = x_url };

                                listXml.Add(object_list);

                                ListView.DataSource = listXml;
                                ListView.DataBind();
                            }
                        }
                        
                    }

                   
                }

            }
            catch
            {
                throw;
            }


        }


    }
}